﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace Esst.Dto.Response
{
    /// <summary>
    /// 响应列表
    /// </summary>
    /// <typeparam name="ItemInfo"></typeparam>
    public class ListResponse<ItemInfo>
    {
        public static ListResponse<ItemInfo> Empty => new(null, 0);

        public ListResponse(IEnumerable<ItemInfo>? list) : this(list, (int?)null)
        {
        }

        public ListResponse(IEnumerable<ItemInfo>? list, PageInfo? pageInfo) : this(list, pageInfo?.RecordCount)
        {
        }

        protected ListResponse(IEnumerable<ItemInfo>? list, int? totalCount)
        {
            List = list ?? Array.Empty<ItemInfo>();
            TotalCount = totalCount ?? list?.Count() ?? 0;
        }

        /// <summary>
        /// 列表信息
        /// </summary>
        public IEnumerable<ItemInfo> List { get; }
        /// <summary>
        /// 总计记录数
        /// </summary>
        public int TotalCount { get; }


        public static implicit operator ListResponse<ItemInfo>(List<ItemInfo> message)
        {
            return new(message);
        }

        public static implicit operator ListResponse<ItemInfo>(ItemInfo[] message)
        {
            return new(message);
        }
    }
}